package exportExcel;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import poi.*;
import testData.TestData;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/11.
 */
public class DetectExcelWorker implements ExWorker {
    private SXSSFWorkbook workbook = new SXSSFWorkbook(-1);
    private List<ExSheet> sheets = new ArrayList<ExSheet>();
    private ExSheet exSheet = new ExSheet();
    private List<ExCell> summaryCellList = Lists.newLinkedList();

    private Map<String, CellStyle> mapCellStyle = Maps.newHashMap();

    private Integer[] headIndex = new Integer[]{0, 0};
    private int currentRow = 0;

    /**
     * build Summary And Data
     *
     * @throws IOException IOException
     */
    public void buildSummaryAndData(Map<String, Integer> tencetFaceCount, Map<String, Integer> baiduFaceCount) throws IOException {
        mapCellStyle = CellStyleUtil.getStyle(this.getCurrentWorkbook());

        Integer[] multiPeople = TestData.multiPeople300;

        List<ExCell> exCellList = Lists.newArrayList();
        exCellList.add(ExUtil.fillToCell(new Integer[]{headIndex[0], headIndex[0]}, "Tencet",
                ExCellType.TEXT, mapCellStyle.get(CellStyleType.head_lightBlue.toString())));
        exCellList.add(ExUtil.fillToCell(new Integer[]{headIndex[0], headIndex[0] + 1}, "Baidu",
                ExCellType.TEXT, mapCellStyle.get(CellStyleType.head_lightBlue.toString())));
        exCellList.add(ExUtil.fillToCell(new Integer[]{headIndex[0], headIndex[0] + 2}, "Actual",
                ExCellType.TEXT, mapCellStyle.get(CellStyleType.head_lightBlue.toString())));

        int row = 1;
        int baiduAcc = 0;
        int tecentAcc = 0;
        int tecentErr = 0;
        int baiduErr = 0;

        for (int i = 0; i < multiPeople.length; i++) {
            exCellList.add(ExUtil.fillToCell(new Integer[]{row, headIndex[0]}, String.valueOf(tencetFaceCount.get(String.valueOf(i))),
                    ExCellType.TEXT, mapCellStyle.get(CellStyleType.summary_content_testItems_1.toString())));
            exCellList.add(ExUtil.fillToCell(new Integer[]{row, headIndex[0] + 1}, String.valueOf(baiduFaceCount.get(String.valueOf(i))),
                    ExCellType.TEXT, mapCellStyle.get(CellStyleType.summary_content_testItems_1.toString())));
            exCellList.add(ExUtil.fillToCell(new Integer[]{row, headIndex[0] + 2}, String.valueOf(multiPeople[i]),
                    ExCellType.TEXT, mapCellStyle.get(CellStyleType.summary_content_testItems_1.toString())));
            if (tencetFaceCount.get(String.valueOf(i)) == multiPeople[i]) {
                tecentAcc += 1;
            } else if (tencetFaceCount.get(String.valueOf(i)) == -1) {
                tecentErr += 1;
            }
            if (baiduFaceCount.get(String.valueOf(i)) == multiPeople[i]) {
                baiduAcc += 1;
            } else if (baiduFaceCount.get(String.valueOf(i)) == -1) {
                baiduErr += 1;
            }
            row++;
        }
        exCellList.add(ExUtil.fillToCell(new Integer[]{row, headIndex[0]}, "Tencet Acc",
                ExCellType.TEXT, mapCellStyle.get(CellStyleType.head_lightBlue.toString())));
        exCellList.add(ExUtil.fillToCell(new Integer[]{row, headIndex[0] + 1}, String.valueOf(tecentAcc * 100 / multiPeople.length) + "%",
                ExCellType.TEXT, mapCellStyle.get(CellStyleType.summary_content_testItems_1.toString())));
        exCellList.add(ExUtil.fillToCell(new Integer[]{row, headIndex[0] + 2}, "Tencet Err",
                ExCellType.TEXT, mapCellStyle.get(CellStyleType.head_lightBlue.toString())));
        exCellList.add(ExUtil.fillToCell(new Integer[]{row, headIndex[0] + 3}, String.valueOf(tecentErr * 100 / multiPeople.length) + "%",
                ExCellType.TEXT, mapCellStyle.get(CellStyleType.summary_content_testItems_1.toString())));

        row++;

        exCellList.add(ExUtil.fillToCell(new Integer[]{row, headIndex[0]}, "Baidu Acc",
                ExCellType.TEXT, mapCellStyle.get(CellStyleType.head_lightBlue.toString())));
        exCellList.add(ExUtil.fillToCell(new Integer[]{row, headIndex[0] + 1}, String.valueOf(baiduAcc * 100 / multiPeople.length) + "%",
                ExCellType.TEXT, mapCellStyle.get(CellStyleType.summary_content_testItems_1.toString())));
        exCellList.add(ExUtil.fillToCell(new Integer[]{row, headIndex[0] + 2}, "Baidu Err",
                ExCellType.TEXT, mapCellStyle.get(CellStyleType.head_lightBlue.toString())));
        exCellList.add(ExUtil.fillToCell(new Integer[]{row, headIndex[0] + 3}, String.valueOf(baiduErr * 100 / multiPeople.length) + "%",
                ExCellType.TEXT, mapCellStyle.get(CellStyleType.summary_content_testItems_1.toString())));

        exSheet.setExCells(exCellList);
        exSheet.setName("Summary");
        sheets.add(exSheet);
        exSheet = null;
        summaryCellList = null;
    }

    /**
     * clear worker up
     */
    public void cleanExportWorker() {
        sheets.clear();
        workbook = null;
        CellStyleUtil.clearResultMap();
    }

    @Override
    public SXSSFWorkbook getCurrentWorkbook() {
        return workbook;
    }

    @Override
    public List<ExSheet> getSheets() {
        return sheets;
    }
}
